<?php
abstract class system_setting_abstract extends system_status {
	
	protected $_config = array();
	public function init($name, array $default) {
		if(1 && $config = @include(_DIRECTORY_SETTING . DIRECTORY_SEPARATOR . $name . '.php')) {
			$this->_config = $config;
			$i = $def = 1;
			foreach($this->_config AS $key => $value) {
				$this->set($i, $key, (isset($value['name']) ? $value['name'] : $key));
				if(isset($value['default']) && $value['default'] == true) {
					$def = $i;
				}
				$i++;
			}
			$this->setDefault($def);
		} else {
			$this->set($default['id'], $default['name'], $default['value']);
			$this->setDefault($default['id']);
		}
	}
	
	public function __call($method, array $args) {
		if(preg_match('/^get(\w+?)$/', $method, $matches)) {
			$_id = strtolower(trim(preg_replace('/([A-Z]{1})/sU', '_$1', $matches[1]), '_'));
			return $this->get($_id, (isset($args[0]) ? $args[0] : null));
		}
		return null;
	}
	
	public function get($name, $id = null) {
		$key = strtolower($this->getKey($id));
		if(!isset($this->_config[$key])) {
			return null;
		}
		$name = strtolower($name);
		return isset($this->_config[$key][$name]) ? $this->_config[$key][$name] : null;
	}
	
}

